<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test concurrent</title>
<script type="text/javascript" src="../unJs/core.js"></script>
<script type="text/javascript" src="../unJs/comet.js"></script>
<script type="text/javascript" src="../unJs/spinner.js"></script>
<link href="../unJs/style/grey/css/reset.css" type="text/css"
	rel="stylesheet" />
<link href="../unJs/style/grey/css/spinner.css" type="text/css"
	rel="stylesheet" />
<script type="text/javascript">
	Un.ready(function() {
		var consloe = Un.Element.get("consloe");
		consloe.println = function(text) {
			var textDiv = new Un.Element("div");
			textDiv.setInnerHtml(text);
			this.appendChild(textDiv);
		}
		var connections = []
		function createOneConnection(callback) {
			var comet = new Un.Comet({
				url : "/cometChat/testConcurrentHandler.comet",
				async : true,
				accept : function(data) {
					consloe.println(new Date() + " 接受到信息:" + data)
				}
			});
			var id = Un.getId();
			var date = new Date();
			id = id + date.getTime();
			comet.connection({
				userId : id
			}, function() {
				if (callback) {
					callback.call(this);
				}
			});
			connections[connections.length] = comet;
		}
		function createConnection(count) {
			for ( var i = 0; i < count; i++) {
				createOneConnection();
			}
		}

		var countInput = new Un.Input.Text({
			type : "number",
			e : Un.getById("concurrentCount")
		});

		var messageInput = new Un.Input.Text({
			e : Un.getById("message")
		});

		//println("测试控制台");

		var connectionBtn = Un.Element.get('connection');
		connectionBtn.addEventListener('click', function() {
			var count = countInput.getValue();
			if (!count) {
				alert("请输入连接数目。")
				return;
			}

			createConnection(count);
			consloe.println("已经创建[" + count + "]个连接。");
		});

		var sendBtn = Un.Element.get('sendMessage');
		sendBtn.addEventListener('click', function() {
			var text = messageInput.getValue();
			Un.AjaxUtils.request({
				url : "/cometChat/testConcurrent.do?method=sendMessageToAll",
				param : {
					message : text
				}
			});
		});

		Un.window.addListener("beforeunload", function() {
			for ( var i = 0; i < connections.length; i++) {
				var comet = connections[i];
				comet.disconnect();
			}
		});
	});
</script>
<style type="text/css">
.layout {
	padding: 20px 20px 0 20px;
}

.consloe {
	border: solid 1px #ccc;
	padding: 5px;
}

.u_btn {
	padding: 2px 5px;
}

.input_count {

}
</style>

</head>
<body>
	<div class="layout">
		<p>
			<label for="concurrentCount">并发数量:</label> <input
				id="concurrentCount" type="text" maxlength="7"
				class="u_input u_input_text input_count" />
			<button id="connection" class="u_btn">链接</button>
		</p>
		<p>
			<label for="message">发送消息:</label> <input id="message" type="text"
				maxlength="7" class="u_input u_input_text input_count" />
			<button id="sendMessage" class="u_btn">发送</button>
		</p>
		<span>consloe</span>
		<div id="consloe" class="consloe"></div>
	</div>
</body>
</html>